package mashibing.class10;

/**
 * 发现第一个相交节点(单链表或环链表)
 *
 * @author yujiafan
 * @version 1.0.0
 * @create 2023-05-16
 */
public class Class10_1_FindFirstIntersectNode {

    public static class Node {
        public int value;
        public Node next;

        public Node(int data) {
            this.value = data;
        }
    }

    public static Node getIntersectNode(Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return null;
        }
        Node loop1 = findLoopNode(head1);
        Node loop2 = findLoopNode(head2);
        // 无环
        if (loop1 == null && loop2 == null) {
            return findIntersectWithNoLoop(head1, head2);
        }
        // 都有换
        if (loop1 != null && loop2 != null) {
            return findIntersectWithLoop(head1, loop1, head2, loop2);
        }
        // 一个有环一个无环,不存在相交节点
        return null;
    }

    // 查找第一个环节点
    public static Node findLoopNode(Node head) {
        if (head == null || head.next == null || head.next.next == null) {
            return null;
        }
        Node slow = head.next;
        Node fast = head.next.next;
        // 找出第一次相交
        while (slow != fast) {
            if (fast.next == null || fast.next.next == null) {
                return null;
            }
            slow = slow.next;
            fast = fast.next.next;
        }
        // 相交后fast节点从头开始一步步与slow相遇
        fast = head;
        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return fast;
    }

    // 无环查找相交节点
    public static Node findIntersectWithNoLoop(Node head1, Node head2) {
        if (head1.next == null || head2.next == null) {
            return null;
        }
        // 相交最后一个节点必相等
        int n = 0;
        Node node1 = head1;
        Node node2 = head2;
        while (node1.next != null) {
            n++;
            node1 = node1.next;
        }
        while (node2.next != null) {
            n--;
            node2 = node2.next;
        }
        if (node1 != node2) {
            return null;
        }
        // 长单链表
        node1 = n >= 0 ? head1 : head2;
        // 短单链表
        node2 = node1 == head1 ? head2 : head1;
        n = Math.abs(n);
        while (n != 0) {
            n--;
            node1 = node1.next;
        }
        while (node1 != node2) {
            node1 = node1.next;
            node2 = node2.next;
        }
        return node1;
    }

    // 有环
    public static Node findIntersectWithLoop(Node head1, Node loop1, Node head2, Node loop2) {
        Node node1;
        Node node2;
        // 环节点相等，存在相交
        if (loop1 == loop2) {
            node1 = head1;
            node2 = head2;
            int n = 0;
            while (node1 != loop1) {
                n++;
                node1 = node1.next;
            }
            while (node2 != loop2) {
                n--;
                node2 = node2.next;
            }
            node1 = n >= 0 ? head1 : head2;
            node2 = node1 == head1 ? head2 : head1;
            n = Math.abs(n);
            while (n != 0) {
                n--;
                node1 = node1.next;
            }
            while (node1 != node2) {
                node1 = node1.next;
                node2 = node2.next;
            }
            return node1;
        }
        node1 = loop1.next;
        // 环不相等，遍历环，看看圈中是否包含另一个圈的入口
        while (node1 != loop1) {
            if (node1 == loop2) {
                return loop1;
            }
            node1 = node1.next;
        }
        return null;
    }

    public static void main(String[] args) {
        // 1->2->3->4->5->6->7->null
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);

        // 0->9->8->6->7->null
        Node head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

        // 1->2->3->4->5->6->7->4...
        head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);
        head1.next.next.next.next.next.next = head1.next.next.next; // 7->4

        // 0->9->8->2...
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next; // 8->2
        System.out.println(getIntersectNode(head1, head2).value);

        // 0->9->8->6->4->5->6..
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

    }
}
